﻿// JavaScript Document
var itemList = [
	/**func:调用函数名称,name:在页面上显示的文字名称**/
	{page:'FrameZoomIN',name:'区域（栅栏）修改'},
	{page:'ManYou',name:'区域（栅栏）添加'}
];
//拉框放大
function FrameZoomIN(){
var str='<div style="text-align:center">请选择区域:<select id="epersonal1" ></select></br><input type="button" class="btnbg" value="显示" onclick="look()" /><input type="button" class="btnbg" value="编辑" onclick="setedit()" /><input type="button" class="btnbg" value="保存" onclick="getedit()" /><input type="button" class="btnbg" value="删除" onclick="setd()" /></div><div id="save"></div><div id="save_is"></div>';
showMapMenu('区域（栅栏）修改',str);
$.ajax({
			type:"POST",
			url:"public/ManageMap/itemList.php",
			cache:false,
			data:"option=bound",
			success:function(data){
				if(data!="N"){
					var arr=data.split("_");
				var arr1=new Array();
				for(var i=0;i<arr.length-1;++i){
					arr1[i]=[arr[i].split(";")[0],arr[i].split(";")[1]];
					}
				document.getElementById('epersonal1').innerHTML="";
				AddSelectOption(arr1,"epersonal1");
					}
				else{
					document.getElementById('save_is').innerHTML="<font style='color:#F00'>没有区域信息...</font>";
					}
				}
		});
	function AddSelectOption(array, selectid){
		if(array){
			for(var i=0;i<array.length;++i){
				var oOption = document.createElement('OPTION');
				oOption.text = array[i][1];
				oOption.value = array[i][0];
				document.getElementById(selectid).options.add(oOption); }}}}
				
function look(){
	var a=document.getElementById('epersonal1').value;
	$.ajax({
			type:"POST",
			url:"public/ManageMap/itemList.php",
			cache:false,
			data:"boundid="+a,
			success:function(data){
				if(data!="N"){	
				mapObj.removeAllOverlays();
				var arrpolygon = new Array();
			for(var i=0;i<data.split(';').length-1;++i){
				arrpolygon.push(new MLngLat(data.split(';')[i].split(',')[0],data.split(';')[i].split(',')[1]));
				}
	var areaopt = new MAreaOptions();
	var polygon = new MPolygon(arrpolygon,areaopt);
	mapObj.addOverlay(polygon,true);
					}else{
					document.getElementById('save_is').innerHTML="<font style='color:#F00'>没该有区域信息</font>";}}});}
					
					
					
					
					
					
function setedit(){
	document.getElementById('save').innerHTML='';
var a=document.getElementById('epersonal1').value;
document.getElementById('epersonal1').disabled='disabled';
	$.ajax({
			type:"POST",
			url:"public/ManageMap/itemList.php",
			cache:false,
			data:"boundid="+a,
			success:function(data){
				if(data!="N"){	
				mapObj.removeAllOverlays();
				var arrpolygon = new Array();
			for(var i=0;i<data.split(';').length-1;++i){
				arrpolygon.push(new MLngLat(data.split(';')[i].split(',')[0],data.split(';')[i].split(',')[1]));
				}
	var areaopt = new MAreaOptions();
	areaopt.canShowTip=false;
	areaopt.isEditable=true;
	var polygon = new MPolygon(arrpolygon,areaopt);
	polygon.id="polygon"+a;
	mapObj.addOverlay(polygon,true);		
					}else{
					document.getElementById('save_is').innerHTML="<font style='color:#F00'>没该有区域信息</font>";
						}
				}
		});
	$.ajax({
			type:"POST",
			url:"public/ManageMap/itemList.php",
			cache:false,
			data:"boundedit="+a,
			success:function(data){
				if(data!="N"){
					document.getElementById('save').innerHTML='</br>开始时间：<input id="StartTime" type="text" name="StartTime" class="Wdate" onfocus="WdatePicker({dateFmt:\'HH:mm:ss\'})" style="width:184px" /></br>结束时间：<input type="text" id="EndTime" name="EndTime" class="Wdate" onfocus="WdatePicker({dateFmt:\'HH:mm:ss\'})" style="width:184px" /><br/>';
					document.getElementById('save').innerHTML+='负责单位：';
					document.getElementById('save').innerHTML+='<select id="edepartment"  multiple="multiple"  size="4" style="width:140px" datatype=\'select\'   onblur="SelectedDep(this)" ></select></br>';
					document.getElementById('save').innerHTML+='选择人员：';
					document.getElementById('save').innerHTML+='<select id="epersonal"  multiple="multiple"  disabled="false"><option value="">请先选择单位</option></select></br>';
					document.getElementById('save').innerHTML+='选择车辆：<select id="evehicle"  multiple="multiple"  disabled="false"><option value="">请先选择单位</option></select></br>';
					}else{
					document.getElementById('save_is').innerHTML="<font style='color:#F00'>没该有区域信息</font>";
					return 0;
						}
						
			var ar=data.split("|");
			document.getElementById('StartTime').value=ar[1].split(' ')[1];
			document.getElementById('EndTime').value=ar[2].split(' ')[1];
			$.ajax({
			type:"POST",
			url:"public/ManageMap/itemList.php",
			cache:false,
			data:"option=me",
			success:function(data){
				var arr=data.split("_");
				var arr1=new Array();
				for(var i=0;i<arr.length-1;++i){
					arr1[i]=[arr[i].split(";")[0],arr[i].split(";")[1]];
					}
				document.getElementById('edepartment').innerHTML="";
				AddSelectOption(arr1,"edepartment");
				for(var i=0;i<document.getElementById('edepartment').options.length;++i){
					for(var n=0;n<(ar[3].split(',').length-1);++n){
					if(document.getElementById('edepartment').options[i].value==ar[3].split(',')[n]){
						document.getElementById('edepartment').options[i].selected=true;
						}}
					}
				SelectedDep(document.getElementById('edepartment'));
				if(ar[0].split('_')[1]!=''){
			for(var i=0;i<document.getElementById('epersonal').options.length;++i){
					for(var n=0;n<(ar[0].split('_')[1].split(',').length);++n){
					if(document.getElementById('epersonal').options[i].value==ar[0].split('_')[1].split(',')[n]){
						document.getElementById('epersonal').options[i].selected=true;
						}}
					}}
					if(ar[0].split('_')[0]!=''){
			for(var i=0;i<document.getElementById('evehicle').options.length;++i){
					for(var n=0;n<(ar[0].split('_')[0].split(',').length);++n){
					if(document.getElementById('evehicle').options[i].value==ar[0].split('_')[0].split(',')[n]){
						document.getElementById('evehicle').options[i].selected=true;
						}}
					}	}
				
				
		function AddSelectOption(array, selectid){
		if(array){
			for( var i=0;i<array.length;++i){
				var oOption = document.createElement('OPTION');
				oOption.text = array[i][1];
				oOption.value = array[i][0];
				document.getElementById(selectid).options.add(oOption); 
			}
		}
}
				}
		});
				}
		});
	}	
function getedit(){
		if(document.getElementById('save').innerHTML==''){
		document.getElementById('save_is').innerHTML="<font style='color:#F00'>没有修改不能保存...</font>";
		return 0;
		}
	var StartTime = $('#StartTime').attr("value");
	var EndTime   = $('#EndTime').attr("value");
	if(StartTime == ''){
		document.getElementById('save_is').innerHTML="<font style='color:#F00'>请选择开始时间...</font>";
		return false;
	}else if(EndTime == ''){
		document.getElementById('save_is').innerHTML="<font style='color:#F00'>请选择结束时间...</font>";
		return false;
	}else{
		StartTime="2011-09-08 "+StartTime;
		EndTime="2011-09-08 "+EndTime;
		var sd = new Date(StartTime.replace(/-/g,"\/"));
		var ed = new Date(EndTime.replace(/-/g,"\/"));
		if(sd>=ed){
		document.getElementById('save_is').innerHTML="<font style='color:#F00'>开始时间大于或者等于结束时间...</font>";
			return false;
		}
	}
		var a=document.getElementById('epersonal1').value;
	if(a==""){
		document.getElementById('save_is').innerHTML="<font style='color:#F00'>没有选择区域名字</font>";
	return 0;
		}
	
	
	
	var obj_str="";
	var obj_str1="";
	var obj=document.getElementById('epersonal').options;
	var obj1=document.getElementById('evehicle').options;
	var n=0;
	for(var i=0;i<obj.length;++i){
		if(obj[i].selected==true){
			obj_str+=obj[i].value+",";
			++n;
			}
		}	
	for(var i=0;i<obj1.length;++i){
		if(obj1[i].selected ==true){
			obj_str1+=obj1[i].value+",";
			++n;
			}
		}
if(n==0){
	document.getElementById('save_is').innerHTML="<font style='color:#F00'>没有选中人员或者车辆信息...</font>";
	return 0;
	}	
mapObj.setCurrentMouseTool(PAN_WHEELZOOM);
	var arr=mapObj.getOverlaysByType(MOverlay.TYPE_POLYGON);
	if(arr.length==0){
		return ;
	}
	var regionXY=new Array(); 
	for(var j=0;j<arr[arr.length-1].lnglatArr.length;j++){
		regionXY.push(arr[arr.length-1].lnglatArr[j].lngX+","+arr[arr.length-1].lnglatArr[j].latY+";");
	}
	var str_1="";
	for(var i=0;i<regionXY.length;++i){
		str_1+=regionXY[i];}

$.ajax({
			type:"POST",
			url:"public/ManageMap/itemList.php",
			cache:false,
			data:"option=addedit&name="+a+"&a="+obj_str1.substr(0,obj_str1.length-1)+"&b="+obj_str.substr(0,obj_str.length-1)+"&c="+str_1+"&d="+StartTime+"&e="+EndTime,
			success:function(data){
				if(data=="Y"){
					document.getElementById('save_is').innerHTML="";
					document.getElementById('save').innerHTML="<font style='color:#0000FF'>保存成功...</font>";
					document.getElementById('epersonal1').disabled='';
					}
				else{
					document.getElementById('save_is').innerHTML="<font style='color:#F00'>没有保存成功...</font>";
					}
				}
		});
	
	
	
	
	
	
	
	
	}	


function setd(){
	var a=document.getElementById('epersonal1').value;
	if(a==""){
		document.getElementById('save_is').innerHTML="<font style='color:#00F'>没有选择对象...</font>";
		return 0;
		}
	if(confirm("确定删除么？")){
		$.ajax({
			type:"POST",
			url:"public/ManageMap/itemList.php",
			cache:false,
			data:"del="+a,
			success:function(data){
                  if(data=="Y"){
					  document.getElementById('save').innerHTML="<font style='color:#00F'>删除成功...</font>";
		$.ajax({
			type:"POST",
			url:"public/ManageMap/itemList.php",
			cache:false,
			data:"option=bound",
			success:function(data){
				if(data!="N"){
					var arr=data.split("_");
				var arr1=new Array();
				for(var i=0;i<arr.length-1;++i){
					arr1[i]=[arr[i].split(";")[0],arr[i].split(";")[1]];
					}
				document.getElementById('epersonal1').innerHTML="";
				AddSelectOption(arr1,"epersonal1");
				function AddSelectOption(array, selectid){
		if(array){
			for(var i=0;i<array.length;++i){
				var oOption = document.createElement('OPTION');
				oOption.text = array[i][1];
				oOption.value = array[i][0];
				document.getElementById(selectid).options.add(oOption); }}}
					}
				else{
					document.getElementById('save_is').innerHTML="<font style='color:#F00'>没有区域信息...</font>";
					}
				}
		});
					  }
				  else{
					  document.getElementById('save_is').innerHTML="<font style='color:#F00'>没有删除成功...</font>";
					  }
				  
				}
		});
		}
	}
	
	
	
	
/*----------------------------------------------------------------------------添加栅栏管理--------------------------------------------------------*/

function ManYou(){
	var str='<div style="text-align:center"><input type="button" class="btnbg" value="添加考勤管理区域" onclick="add_p()" /></div><div id="save"></div><div id="save_is"></div>';
showMapMenu('区域（栅栏）添加',str);
	}
function add_p(){
	mapObj.addEventListener(mapObj,ADD_OVERLAY,mouseDrawPolygonMulti_1);
	mapObj.setCurrentMouseTool(DRAW_POLYGON);}
function mouseDrawPolygonMulti_1(){
	var regionXY=new Array();
	mapObj.setCurrentMouseTool(PAN_WHEELZOOM);
	var arr=mapObj.getOverlaysByType(MOverlay.TYPE_POLYGON);
	if(arr.length==0){
		return ;
	}
	for(var j=0;j<arr[arr.length-1].lnglatArr.length;j++){
		regionXY.push(arr[arr.length-1].lnglatArr[j].lngX+","+arr[arr.length-1].lnglatArr[j].latY+";");
	}
	var str="";
	var str_1="";
	for(var i=0;i<regionXY.length;++i){
		str_1+=regionXY[i];
	str+=regionXY[i]+"</br>";}
	document.getElementById('save').innerHTML='保存这个栅栏面数据:</br>开始时间：<input id="StartTime" type="text" name="StartTime" class="Wdate" onfocus="WdatePicker({dateFmt:\'HH:mm:ss\'})" style="width:184px" /></br>结束时间：<input type="text" id="EndTime" name="EndTime" class="Wdate" onfocus="WdatePicker({dateFmt:\'HH:mm:ss\'})" style="width:184px" /><br/>'+str+'<textarea id="save_text"></textarea></br><input type="button"  class="btnbg"  onclick="save(\''+str_1+'\')" value="保存" /><input  class="btnbg"  type="button" value="取消" />';
	mapObj.removeEventListener(mapObj,ADD_OVERLAY,mouseDrawPolygonMulti_1);
	}
function save(str){
	var StartTime = $('#StartTime').attr("value");
	var EndTime   = $('#EndTime').attr("value");
	if(StartTime == ''){
		document.getElementById('save_is').innerHTML="<font style='color:#F00'>请选择开始时间...</font>";
		return false;
	}else if(EndTime == ''){
		document.getElementById('save_is').innerHTML="<font style='color:#F00'>请选择结束时间...</font>";
		return false;
	}else{
		StartTime="2011-09-08 "+StartTime;
		EndTime="2011-09-08 "+EndTime;
		var sd = new Date(StartTime.replace(/-/g,"\/"));
		var ed = new Date(EndTime.replace(/-/g,"\/"));
		if(sd>=ed){
		document.getElementById('save_is').innerHTML="<font style='color:#F00'>开始时间大于或者等于结束时间...</font>";
			return false;
		}
	}
	var a=document.getElementById('save_text').value;
	if(a==""){
		document.getElementById('save_is').innerHTML="<font style='color:#F00'>请输入保存区域的名字</font>";
	return 0;
		}
		document.getElementById('save_is').innerHTML="";
	$.ajax({
			type:"POST",
			url:"public/ManageMap/itemList.php",
			cache:false,
			data:"option=add&name="+a+"&points="+str+"&s="+StartTime+"&e="+EndTime,
			success:function(data){
				if(data=="Y"){
					document.getElementById('save_is').innerHTML="<font style='color:#F00'>区域添加成功请添加人员...</font>";
					document.getElementById('save').innerHTML='区域名字：<input type="text" id="rectname" disabled="disabled" value="'+a+'"/></br>';
					document.getElementById('save').innerHTML+='负责单位：';
					document.getElementById('save').innerHTML+='<select id="edepartment"  multiple="multiple"  size="4" style="width:140px" datatype=\'select\'   onblur="SelectedDep(this)" ></select></br>';
					document.getElementById('save').innerHTML+='选择人员：';
					document.getElementById('save').innerHTML+='<select id="epersonal"  multiple="multiple"  disabled="false"><option value="">请先选择单位</option></select></br>';
					document.getElementById('save').innerHTML+='选择车辆：<select id="evehicle"  multiple="multiple"  disabled="false"><option value="">请先选择单位</option></select></br>';
					document.getElementById('save').innerHTML+='<input type="button" onclick="saverect()"  class="btnbg"  value="保存" /><input type="button"  class="btnbg"  value="取消" />';
					$.ajax({
			type:"POST",
			url:"public/ManageMap/itemList.php",
			cache:false,
			data:"option=me",
			success:function(data){
				var arr=data.split("_");
				var arr1=new Array();
				for(var i=0;i<arr.length-1;++i){
					arr1[i]=[arr[i].split(";")[0],arr[i].split(";")[1]];
					}
				document.getElementById('edepartment').innerHTML="";
				AddSelectOption(arr1,"edepartment");
		function AddSelectOption(array, selectid){
		if(array){
			for( var i=0;i<array.length;++i){
				var oOption = document.createElement('OPTION');
				oOption.text = array[i][1];
				oOption.value = array[i][0];
				document.getElementById(selectid).options.add(oOption); 
			}
		}
}
				}
		});
					
					return 0;
					}else{
					document.getElementById('save_is').innerHTML="<font style='color:#F00'>数据库里面已经存在区域名字请重新输入...</font>";
					return 0;
						}
				}
		});
		
}
function SelectedDep(obj){
	var SelectedDepartmendID = [];
	var dep =obj.options;
	for(var i=0; i<dep.length;i++){
		if(dep[i].selected == true){
			SelectedDepartmendID.push(dep[i].value);
		}
	}
	if(SelectedDepartmendID.length==0){
		document.getElementById('save_is').innerHTML="<font style='color:#F00'>没有选择判处所...</font>";
		return 0;
		}
	$.ajax({
		type:'POST',
		url:'manageAnjian.php',
		async:false,
		data:'act=select&selectedID='+SelectedDepartmendID,
		success:function(data){
			var arr = data.split('|');
		  if(arr[0] !== ''){
				$('#epersonal').html('');
				$('#epersonal').attr('size','4');
				$('#epersonal').attr('multiple','multiple');
				$('#epersonal').attr('disabled','');
				$('#epersonal').html(arr[0]);
			}else{
				$('#epersonal').attr('size','1');
				$('#epersonal').attr('multiple','');
				$('#epersonal').attr('disabled','false');
				$('#epersonal').html('<option value="">请先选择单位</option>');
			}
			//添加车辆
			if(arr[1] !== ''){
				$('#evehicle').html('');
				$('#evehicle').attr('size','4');
				$('#evehicle').attr('multiple','multiple');
				$('#evehicle').attr('disabled','');
				$('#evehicle').html(arr[1]);
				document.getElementById('save_is').innerHTML="";
			}else{
				$('#evehicle').attr('size','1');
				$('#evehicle').attr('multiple','');
				$('#evehicle').attr('disabled','false');
				$('#evehicle').html('<option value="">请先选择单位</option>');
				document.getElementById('save_is').innerHTML+="";
			}
			if(arr[0]==''&&arr[1]== ''){
		document.getElementById('save_is').innerHTML="<font style='color:#F00'>你选择单位没数据...</font>";
				}
		}
	});
}
function saverect(){
	var obj_str="";
	var obj_str1="";
	var obj=document.getElementById('epersonal').options;
	var obj1=document.getElementById('evehicle').options;
	var n=0;
	for(var i=0;i<obj.length;++i){
		if(obj[i].selected==true){
			obj_str+=obj[i].value+",";
			++n;
			}
		}	
	for(var i=0;i<obj1.length;++i){
		if(obj1[i].selected ==true){
			obj_str1+=obj1[i].value+",";
			++n;
			}
		}
		
if(n==0){
	document.getElementById('save_is').innerHTML="<font style='color:#F00'>没有选中人员或者车辆信息...</font>";
	return 0;
	}	
	var rect=document.getElementById('rectname').value;
$.ajax({
			type:"POST",
			url:"public/ManageMap/itemList.php",
			cache:false,
			data:"option=addrect&name="+rect+"&a="+obj_str1.substr(0,obj_str1.length-1)+"&b="+obj_str.substr(0,obj_str.length-1),
			success:function(data){
				if(data=="Y"){
					document.getElementById('save_is').innerHTML="";
					document.getElementById('save').innerHTML="<font style='color:#0000FF'>添加成功...</font>"
					}
				else{
					document.getElementById('save_is').innerHTML="<font style='color:#F00'>没有添加成功...</font>"
					}
				}
		});
	}



//-------------------------------------------------------------手机定位----------------------------------------------------
function phoneLocation(){
		//mapObj.addEventListener(mapObj,ZOOM_START,startZoom);
		mapObj.addEventListener(mapObj,MOUSE_UP,startZoom);
}
function startZoom(){
	mapObj.removeOverlaysByType(MOverlay.TYPE_MARKER);
	var zoomL=mapObj.getZoomLevel();
	if(zoomL>11){
		var bounds=mapObj.getLngLatBounds();
		$.ajax({
			type:'POST',
			url:"public/ManageMap/phoneLocation.php",
			cache:false,
			dataType:'json',
			async:false,
			data:'act=location&southWestLngX='+bounds.southWest.lngX+'&southWestLatY='+bounds.southWest.latY+'&northEastLngX='+bounds.northEast.lngX+'&northEastLatY='+bounds.northEast.latY,
			success:function(data){
				if(data.status=='y'){
					//alert(data.info);
				}else{
	var lonlatarr=[];
						for(var n=0;n<data.length;++n){
							lonlatarr.push([data[n].LONGITUDE,data[n].LATITUDE]);
							}
							
				   lonlatarr=gpsToMaps(lonlatarr.join(";"));
				   if(lonlatarr.length==0){
				   	return 0;
				   	}
				for(var n1=0;n1<data.length;++n1){
					data[n1].LONGITUDE=lonlatarr[n1][0];
					data[n1].LATITUDE=lonlatarr[n1][1];
							}
					
					for(var i=0;i<data.length;i++){
						var tipOption=new MTipOptions();
						 tipOption.title="手机获取信息";
					  tipOption.content="人员号："+data[i].PERSONNEL_SN+"<br>"+"内容："+data[i].MAPMARK_INFO;
					  var markerOption = new MMarkerOptions();
					  markerOption.imageUrl="images/lan_1.png";
					  markerOption.rotation=0;
					  //图片锚点BOTTOM_CENTER相对于标注位置的位置
					  markerOption.anchor=new MPoint(0,0);
					  //设置图片相对于加点经纬度坐标的位置。九宫格位置。默认BOTTOM_CENTER代表正下方
					  markerOption.imageAlign=BOTTOM_CENTER;
					  //设置点的标注参数选项
					  markerOption.isEditable=false;
					  //设置点的信息窗口参数选项
					  markerOption.tipOption = tipOption;
					  //是否在地图中显示信息窗口，true，可以显示（默认）；false，不显示
					  markerOption.canShowTip= true;
					  //设置图标旋转的角度
					  markerOption.rotation="0";
					  //通过经纬度坐标及参数选项确定标注信息
					  Mmarker = new MMarker(new MLngLat(data[i].LONGITUDE,data[i].LATITUDE),markerOption);
					  mapObj.addOverlay(Mmarker,true) ;
					}
					
				}
			}
		});
	}
}